package com.ruoyi.system.service.impl;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.SysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品信息 业务层处理
 */
@Service
public class SysGoodsServiceImpl implements SysGoodsService {
    @Autowired
    private SysGoodsMapper goodsMapper;
    @Override
    public List<SysGoods> selectGoodsAll(SysGoods sysGoods) {
        return goodsMapper.selectGoodsAll(sysGoods);
    }

    @Override
    public int insertGoods(SysGoods sysGoods) {
        String code=goodsMapper.queryMaxCode();
        if(null==code){
            sysGoods.setCode("sx3401");
        }else{
            int tmp=Integer.parseInt(code.split("x")[1])+1;
            sysGoods.setCode("sx"+tmp);
        }
        return goodsMapper.insertGoods(sysGoods);
    }

    @Override
    public String land(HttpServletRequest request) {
        List list=goodsMapper.selectGoodsAll(null);
        String path= request.getSession().getServletContext().getRealPath("/");
        String fileName="商品.xlsx";
        String name=path+fileName;
        EasyExcel.write(name,SysGoods.class).sheet("商品信息").doWrite(list);
        return name;
    }
}
